<template>
  <el-dialog
     title="床位交换"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" size="small"  :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="客户姓名" prop="name">
        <el-input  readonly v-model="dataForm.name" placeholder="客户姓名"></el-input>
      </el-form-item>
      <el-form-item label="客户性别" prop="sex">
        <el-input  readonly  v-model="dataForm.sex" placeholder="客户性别"></el-input>
      </el-form-item>
      <el-form-item label="旧床位" prop="beddetails">
        <el-input   readonly  v-model="dataForm.beddetails" placeholder="旧床位"></el-input>
      </el-form-item>
      <el-form-item label="新楼号" prop="buildingNo">
        <el-input   readonly  v-model="dataForm.buildingNo" placeholder="新楼号"></el-input>
      </el-form-item>
      <el-form-item label="楼层" prop="bedFloor">
        <el-select v-model="dataForm.bedFloor"  @change="floorchange" placeholder="请选择楼层">
          <el-option
            v-for="item in floorNameList"
            :key="item.roomfloor"
            :label="item.roomfloor"
            :value="item.roomfloor">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="新房间" prop="newRoomNo">
        <el-select v-model="dataForm.newRoomNo" @change="roomchange" placeholder="请选择新房间">
          <el-option
            v-for="item in roomList"
            :key="item.id"
            :label="item.roomno"
            :value="item.roomno">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="新床位" prop="newBedId">
        <el-select v-model="dataForm.newBedId" placeholder="请选择新床位">
          <el-option
            v-for="item in bedList"
            :key="item.id"
            :label="item.bedno"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="开始日期" prop="startdate">
      <!-- <el-input v-model="dataForm.startdate" placeholder="开始日期"></el-input> -->
      <el-date-picker
        v-model="dataForm.startdate"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="结束日期" prop="enddate">
      <!-- <el-input v-model="dataForm.enddate" placeholder="结束日期"></el-input> -->
      <el-date-picker
        v-model="dataForm.enddate"
        type="date"
        placeholder="选择日期">
      </el-date-picker>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          startdate: '',
          enddate: '',
          beddetails: '',
          customerid: '',
          bedid: '',
          isdelete: '',

          name:'系统管理员',
          sex:'男',
          buildingNo:'606',
          bedFloor:'',
          newRoomNo:'',
          newBedNo:'',//不再使用
          newBedId:'',
        },
        floorNameList:[], //楼层列表
        roomList:[],//房间列表
        bedList:[],//床位列表
        dataRule: {
          startdate: [
            { required: true, message: '开始日期不能为空', trigger: 'blur' }
          ],
          enddate: [
            { required: true, message: '结束日期不能为空', trigger: 'blur' }
          ],
          newBedId: [
            { required: true, message: '床位id不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '客户姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '客户性别不能为空', trigger: 'blur' }
          ],
          bedFloor: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ]
        }
      }
    },

    created(){
      this.getFloorName();
    },
    methods: {
      //获取选择楼层，并根据楼层获取房间号
      floorchange(event){
        this.getRoomListByFloorName(event)

      }, 
      //获取新房间，并根据楼层获取新床位
       roomchange(event){
        this.getBedListByRoomNo(event)
      },
      getBedListByRoomNo(newRoomNo){
          //清空bedList
          this.bedList=[];
        //清空newBedNo
        this.dataForm.newBedNo='';
        this.$http({
              url: this.$http.adornUrl(`/bed/bed/bedList/${newRoomNo}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.bedList=data.bedList;
                console.log(this.bedList);
              }
            })
      },
      
      getRoomListByFloorName(floorName){

        //清空roomList
        this.roomList=[];
        //清空roomno
        this.dataForm.newRoomNo='';
        this.$http({
              url: this.$http.adornUrl(`/room/room/roomList/`),
              method: 'post',
              params: this.$http.adornParams({
                'floorName':floorName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.roomList=data.roomList;
                console.log(this.roomList);
              }
            })
      },
      //获取楼层列表
      getFloorName(){
        this.$http({
              url: this.$http.adornUrl(`/room/room/floorNameList/`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.floorNameList=data.floorNameList
                console.log(this.floorNameList)
              }
            })
      },

      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/beddetails/beddetails/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.startdate = data.bedDetails.startdate
                this.dataForm.enddate = data.bedDetails.enddate
                this.dataForm.beddetails = data.bedDetails.beddetails
                this.dataForm.customerid = data.bedDetails.customerid
                this.dataForm.bedid = data.bedDetails.bedid
                this.dataForm.isdelete = data.bedDetails.isdelete
                this.dataForm.name=data.bedDetails.name
                this.dataForm.sex=data.bedDetails.sex
                this.dataForm.id=data.bedDetails.id
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/beddetails/beddetails/bedExchange`),
              method: 'post',
              data: this.$http.adornData({
                // 'id': this.dataForm.id || undefined,
                // 'startdate': this.dataForm.startdate,
                // 'enddate': this.dataForm.enddate,
                // 'beddetails': this.dataForm.beddetails,
                // 'customerid': this.dataForm.customerid,
                // 'bedid': this.dataForm.bedid,
                // 'isdelete': this.dataForm.isdelete
                buildingNo:this.dataForm.buildingNo,
                customerId:this.dataForm.customerid,
                endDate:this.dataForm.enddate,
                id:this.dataForm.id,
                newBedId:this.dataForm.newBedId,
                newRoomNo:this.dataForm.newRoomNo,
                oldBedId:this.dataForm.bedid,
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
